package com.fs.controller;

import com.fs.param.MonitorFalconDynamicParam;
import com.fs.service.MonitorFalconService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by fengshuai on 17/5/18.
 */
@Controller
@RequestMapping("/falcon")
public class MonitorFalconController {
    protected static Logger logger = Logger.getLogger(MonitorFalconController.class);

    @Autowired
    private MonitorFalconService monitorFalconService;

    @RequestMapping("/info")
    @ResponseBody
    public ModelMap info(@RequestParam(value = "day", defaultValue = "0") int day,
                         @RequestParam(value="dbId", defaultValue = "0") int dbId,
                         @RequestParam(value = "segment",defaultValue = "0") int segment,
                         HttpServletResponse response) {
        ModelMap modelMap = new ModelMap();
        try {
            response.addHeader("Access-Control-Allow-Origin", "*");
            MonitorFalconDynamicParam param = new MonitorFalconDynamicParam();
            param.setDay(day);
            param.setDbId(dbId);
            param.setSegment(segment);
            modelMap.addAttribute("success", true);
            modelMap.addAttribute("data", monitorFalconService.batchGetByDynamicParam(param));
            return modelMap;
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return modelMap.addAttribute("success", false).addAttribute("message", e.getMessage());
        }
    }
}
